<template>
  <div class="upload-container">
  <el-upload
      class="avatar-uploader"
      :action="`${API_URL}/dish/upload`"
      :show-file-list="false"
      :on-success="handleAvatarSuccess"
      :before-upload="beforeAvatarUpload"
      name="file"
  >
    <img v-if="imageUrl || viewImage" :src="`${API_URL}/dingdang/${ imageUrl || viewImage}`" class="avatar" />
    <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
  </el-upload>
  </div>
</template>

<script setup name="uploadFile">

import { ref, defineEmits, defineProps } from 'vue';
import { Plus } from '@element-plus/icons-vue';
import { ElMessage } from 'element-plus';
import { useUploadFile } from '../hooks/file/useUploadFile';
import { API_URL } from '../hooks/request/const';

const emit = defineEmits(['getImage']);
defineProps({
  page: {
    type: String,
    default: 'add',
  },
  viewImage: {
    type: String,
    default: '',
  },
});
const { beforeAvatarUpload } = useUploadFile();
const imageUrl = ref('');
function handleAvatarSuccess(response) {
  if (response.data) {
    ElMessage.success(response.msg);
    imageUrl.value = response.data;
    emit('getImage', imageUrl);
  }
}

</script>

<style scoped>
.avatar-uploader .avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
}
</style>
